//
//  ZDCollectionViewFlowLayout.m
//  ZDEmojiKeyBoard
//
//  Created by Apple on 15/11/5.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZDCollectionViewFlowLayout.h"

@implementation ZDCollectionViewFlowLayout

//重写prepareLayout方法  在该方法内部可以拿到collectionView的准确frame
- (void)prepareLayout
{
    [super prepareLayout];
    
    CGFloat width = self.collectionView.bounds.size.width / 7;
    CGFloat height = self.collectionView.bounds.size.height / 3;
    
    self.itemSize = CGSizeMake(width, height);
    self.minimumLineSpacing = 0;
    self.minimumInteritemSpacing = 0;
    
    //设置滚动方向 ———— 水平滚动
    self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    //设置分页
    self.collectionView.pagingEnabled = YES;
    
    //隐藏水平滚动条
    self.collectionView.showsHorizontalScrollIndicator = NO;
}

@end


@interface ZDCollectionViewCell ()

@property (nonatomic, strong) UIButton *emojiBtn;

@end

@implementation ZDCollectionViewCell


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self prepareUI];
    }
    return self;
}

- (void)prepareUI
{
    [self.contentView addSubview:self.emojiBtn];
    
    //设置按钮的每一边 都是cell的内插5个点
    self.emojiBtn.frame = CGRectInset(self.bounds, 5, 5);
}


- (void)setEmoji:(ZDEmoji *)emoji
{
    _emoji = emoji;
    
    //这段代码不能放在这，因为如果是删除按钮，在这里设置的图片，执行到下面会因为条件不成立被置空，而如果在这里增加return，则会因为下面两种情况不成立，没有置空而无法避免cell的重用带来的影响
//    if (emoji.isRemoveBtn)
//    {
//        [self.emojiBtn setImage:[UIImage imageNamed:@"compose_emotion_delete"] forState:UIControlStateNormal];
//    }

    [self.emojiBtn setImage:emoji.emojiImage forState:UIControlStateNormal];
    
    [self.emojiBtn setTitle:emoji.emojiString forState:UIControlStateNormal];
    
    if (emoji.isRemoveBtn)
    {
        [self.emojiBtn setImage:[UIImage imageNamed:@"compose_emotion_delete"] forState:UIControlStateNormal];
    }
}


#pragma mark 懒加载
- (UIButton *)emojiBtn
{
    if (!_emojiBtn)
    {
        _emojiBtn = [[UIButton alloc] init];
        
//        _emojiBtn.backgroundColor = [UIColor blackColor];
        
        _emojiBtn.titleLabel.font = [UIFont systemFontOfSize:32];
        
        //关闭按钮的用户交互
        _emojiBtn.userInteractionEnabled = NO;
    }
    
    return _emojiBtn;
}

@end